<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/

/**
 * @returns integer
 * @returns number of whatever in DB
 * @param cusers true 
 * @param cposts true
 * @param ctags true  
 * @param uid true 
 * @param tagid true 
 */

function conversations_userapi_count($args)
{
    extract($args);

    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    // Let's get jiggy with the counting.  We'll count on every table that is called.
    if (!empty($cuid)){
        $table = $xartable['cusers'];
    } elseif ((!empty($ctagid)) and (!empty($cpostid))) {
        $table = $xartable['clook']; 
    } elseif (!empty($cposts)){
        $table = $xartable['cposts'];        
    } elseif (!empty($ctags)) {
        $table = $xartable['ctags']; 

    }

    // And we can also count on conditions IE number of posts via users.  This will be fun.
    // Number of tags by user -- ctags by uid
    // Number of posts by user -- cposts by uid
    // Number of posts in tag -- cposts by tagid
    $wheres = array();
    if ((!empty($ctagid)) && (!empty($cuid))) {
        $wheres[] = "xar_uid = " . (int)$cuid;
    }
    if ((!empty($cpostid)) && (!empty($cuid))) {
        $wheres[] = "xar_uid = " . (int)$cuid;
    }
    if ((!empty($cpostid)) && (!empty($ctagid))) {
        $wheres[] = "xar_tagid = " . (int)$ctagid;
    }
    // return var_export($wheres);
    if (count($wheres) > 0) {
        $where = " WHERE " . join(" AND ", $wheres)." ";
    } else {
        $where = "";
    }

    // Get Count
    $query = "SELECT COUNT(1) FROM $table $where";
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    list($numitems) = $result->fields;
    $result->Close();
    return $numitems;
}
?>